//
//  DeviceSettingViewController.m
//  ZhongKeXinHeater
//
//  Created by Kendama on 2017/6/6.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "DeviceSettingViewController.h"

@interface DeviceSettingViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation DeviceSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)refreshBaseControlValue {
    
    [self setLeftButtonImage:[UIImage imageNamed:@"nav_icon_back"]];
    [self setNavigationTitle:@"设置" titleColor:[UIColor blackColor]];
    self.navigationBar.backgroundColor = [UIColor colorWithHexString:@"D0D0D0"];
    [self masLayoutSubviews];
}





#pragma mark -- 事件
- (void)checkUpdate {
    [SVProgressHUD showWithStatus:@"正在检查版本更新..."];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[DTCloudManager defaultJNI_iOS_SDK]getFirmwareVersionByMacAddress:CurrentDevice.macAddress deviceType:DeviceUsedByWiFi protocolType:DTSDKProtocolTypeThird resultCallback:^(NSDictionary *dic) {
            [SVProgressHUD dismiss];
            if ([dic[@"errcode"] isEqualToString:@"0"]) { // 有新版本
                
                [[AlertViewManager shareManager]initWithMessage:@"有新的固件版本，是否更新固件" cancleButtonTitle:@"取消" OtherButtonsArray:@[@"确定"] clickAtIndex:^(NSInteger buttonIndex, UIAlertView *alertView) {
                    if (buttonIndex == 1) {
                        [self updateFirmwareVersions];
                    }
                }];
            } else if ([dic[@"errcode"] isEqualToString:@"1"]) { // 设备正在进行固件升级
                [SVProgressHUD showErrorWithStatus:dic[@"errmsg"]];
            } else if ([dic[@"errcode"] isEqualToString:@"2"]) { // 固件已是最新版本
                [SVProgressHUD showSuccessWithStatus:dic[@"errmsg"]];
            } else {
                [SVProgressHUD showErrorWithStatus:dic[@"errmsg"]];
            }
        }];
    });
}

- (void)updateFirmwareVersions{
    [SVProgressHUD showWithStatus:@"正在升级中..."];
    [[DTCloudManager defaultJNI_iOS_SDK]updateFirmwareVersionByMacAddress:CurrentDevice.macAddress deviceType:DeviceUsedByWiFi protocolType:DTSDKProtocolTypeThird resultCallback:^(NSDictionary *dic) {
        [SVProgressHUD showWithStatus:dic[@"errmsg"]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
    }];
}


#pragma mark -- 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return px1920Height(154);
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"DeviceSettingCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

    // 分割线对齐左边
    cell.separatorInset = UIEdgeInsetsZero;
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        cell.textLabel.text = @"固件升级";
    } else if (indexPath.row == 1) {
        cell.textLabel.text = @"账号分享";
    }
    cell.textLabel.font = [UIFont systemFontOfSize:px1080Width(42)];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        [self checkUpdate];
    } else if (indexPath.row == 1) {
        self.hidesBottomBarWhenPushed = YES;
        ShareViewController *vc = [[ShareViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    }

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}



#pragma mark -- 懒加载
- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        [superView addSubview:_tableView];
    }
    return _tableView;
}


#pragma mark -- 初始化
- (void)masLayoutSubviews{
    __weak UIView *superView = self.view;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(superView).with.offset(64);
        make.width.equalTo(superView);
        make.bottom.equalTo(superView);
    }];
}

@end
